﻿// JScript File

//////////////////////////////////////////////////////////////////////
//admingallery.aspx
function KcmsShowUpload()
{
	var tds = document.getElementById("tdFileUpload").style;
	if(tds.visibility == "visible")
	{
		tds.visibility = "hidden";
	}
	else
	{
		tds.visibility = "visible";
	}
}
function KcmsImageDoubleClick(root, filename, imgwidth, imgheight)
{
	var KcmsPreviewWin = window.open('','imgPreview','width=300,height=300, resizable=yes, status=yes');
	if(KcmsPreviewWin)
	{
		KcmsPreviewWin.document.open();
	}
	else
	{
		alert('Please turn off your PopUp blocking software');
		return;
	}
	KcmsPreviewWin.document.write("<html><body style='margin:0px; padding:10px; font: 8pt verdana;'> " +
		"<div align='center'><img src='" + root + "/" + filename + "'/></div>" +
		"<div align='center'>" + filename + "<br/>" + imgwidth + "x" + imgheight + "</div></body></html>");
	KcmsPreviewWin.document.close();
	KcmsPreviewWin.resizeTo(imgwidth + 30 ,imgheight + 110);
	KcmsPreviewWin.focus();
}
///////////////////////////////////////////////////////////////////////
//productedit.aspx
function validateandsubmit()
{
	var ind = new Array("errorSalePrice", "errorOldPrice", "errorDiscount", "errorWeight", "errorUnitsInStock", "errorAddUnits");
	var fields = new Array("txtSalePrice", "txtOldPrice", "txtDiscount", "txtWeight", "txtUnitsInStock", "txtAddUnits");
	var types = new Array("n2", "n2", "n0", "n1", "n0", "n0");

	for(var nn in ind)
	{
		document.getElementById(ind[nn]).style.visibility = "hidden"; //reset all indicators
	}
	var isvalid = true;
	for(var n = 0; n < fields.length; n++)
	{
		var elem = document.getElementById("ctl00_ContentPlaceHolder1_" + fields[n]	);
		var indikator = document.getElementById(ind[n]);
		if( ! Validate(elem.value, indikator, types[n])) isvalid = false;
	}
	if(isvalid)
	{
		KcmsToolbarClick('save', 'dont ask');
	}
	else
	{
		alert("Some values are not valid. Check values marked red.");
	}
}
function Validate(value, indikator, type)
{
	var regex;
	switch(type)
	{
		case "n2":
		regex = /^[0-9]+\.{0,1}[0-9]{0,2}$/ ;
		break;
		
		case "n1":
		regex = /^[0-9]+\.{0,1}[0-9]{0,1}$/;
		break;
		
		case "n0":
		regex = /^[0-9]+$/;
		break;
	}
	return IsMatch(value, indikator, regex);
}
function IsMatch(value, indikator, regex)
{
	if(value.match(regex))
	{
		return true;
	}
	else
	{
		indikator.style.visibility = "visible";
		return false;
	}
}
	
///////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// global
function carover(obj)
{
	obj.className = "trmouseover";
}
function carout(obj)
{
	obj.className = "trmouseout";
}

/////////////////////////////////////////////////////////
//shippingsdetails.aspx

function submitchangesA()
{
	var f1 = document.getElementById("ctl00_ContentPlaceHolder1_TextBox2");
	var f2 = document.getElementById("ctl00_ContentPlaceHolder1_TextBox3");
	var v1 = document.getElementById("valid1");
	var v2 = document.getElementById("valid2");
	v1.style.visibility="hidden";
	v2.style.visibility="hidden";
	var invalid = false;
	if( ! valid( f1.value)) { v1.style.visibility="visible"; invalid = true;}
	if( ! valid( f2.value)) { v2.style.visibility="visible"; invalid = true;}
	if(invalid) return;
	document.getElementById("ctl00_ContentPlaceHolder1_hdnCommand").value = "save";
	KcmsDoPostBack();
}
function valid(objval)
{
	var s = objval.match(/^[0-9]+\.{0,1}[0-9]{0,2}$/);
	return s;
}

//////////////////////////////////////////////////////////
// global

function carselect(evnt, loc)
{
	if((evnt.target) && ((evnt.target.className == "noclick") || (evnt.target.tagName == "INPUT")) )
	{
		return;
	}
	if((evnt.srcElement) && ((evnt.srcElement.className == "noclick") || ( evnt.srcElement.tagName == "INPUT")))
	{
		return;
	}
	window.location = loc;
}


///////////////////////////////////////////////////////////
//productedit.aspx
function loadPix(txtobj, imgobj) 
{
	window.txtval = document.getElementById("ctl00_ContentPlaceHolder1_" + txtobj);
	window.srcval = document.getElementById(imgobj);
	var win = open("pop.gallery.aspx", "gallery", "width=720px, height=600px, scrollbars=yes, resizable=yes");
	
}

function editdesign(control)
{
	window.holderMode = "design";
	window.holderControl1 = document.getElementById("ctl00_ContentPlaceHolder1_ltr" + control);
	window.holderControl2 = document.getElementById("ctl00_ContentPlaceHolder1_hdn" + control);
	var win = window.open("pop.designer.aspx", "editor", "width=600px, height=400px, resizable=yes, status=no");
	win.focus();	
}

function edithtml(control)
{
	window.holderMode = "html";
	window.holderControl1 = document.getElementById("ctl00_ContentPlaceHolder1_ltr" + control);
	window.holderControl2 = document.getElementById("ctl00_ContentPlaceHolder1_hdn" + control);
	var win = window.open("pop.designer.aspx", "editor", "width=600px, height=400px, resizable=yes, status=no");
	win.focus();	
}

///////////////////////////////////////////////////////////
//orders.aspx

function opendetails(evnt, orderid)
{
	window.location = "orderdetails.aspx?oid=" + orderid;
}

///////////////////////////////////////////////////////////
//orderdetails.aspx
function showwin(link)
{
	window.open(link, "details").focus();
}
function goback()
{
	window.history.back();
}


////////////////////////////////////////////////////////////
function showhtmlhelp(htmltext)
{
	var msg = htmltext;
	var KcmsPreviewWin = window.open('','help','width=500,height=200, resizable=yes, status=yes');
	if(KcmsPreviewWin)
	{
		KcmsPreviewWin.document.open();
	}
	else
	{
		alert('Please turn off your PopUp blocking software');
		return;
	}
	KcmsPreviewWin.document.write("<html><body style='margin:0px; padding:10px; font: 8pt verdana;'>" + msg + "</body></html>");
	KcmsPreviewWin.document.close();
	KcmsPreviewWin.focus();
}

////////////////////////////////////////////////////////////
//admingallery.asx

function KcmsToolbarSave()
{
	document.getElementById("ctl00_ContentPlaceHolder1_hdnCommand").value = "save";
	KcmsDoPostBack();
}	

function showhelp(img)
{
	var msg = img.title;
	var KcmsPreviewWin = window.open('','help','width=500,height=200, resizable=yes, status=yes');
	if(KcmsPreviewWin)
	{
		KcmsPreviewWin.document.open();
	}
	else
	{
		alert('Please turn off your PopUp blocking software');
		return;
	}
	KcmsPreviewWin.document.write("<html><body style='margin:0px; padding:10px; font: 8pt verdana;'>" + msg + "</body></html>");
	KcmsPreviewWin.document.close();
	KcmsPreviewWin.focus();
}
///////////////////////////////////////////////////////////////////////

function KcmsReturnUrl()
{
	window.history.back();
}

function initializeadmin()
{
	resizetable();
	
	var elem = document.getElementsByTagName("TR");
	KcmsSetSelRowEvents(elem);
	var elem = document.getElementsByTagName("TD");
	KcmsSetToolbarEvents(elem);
	
}

function resizetable()
{
	//resize main table and lefttreeholder
	//var lth = document.getElementById("lefttreeholder");
	var tt = document.getElementById("outtable");
	var hh;
	if(window.opera)
	{
		hh = document.getElementsByTagName("body")[0];
	}
	else
	{
		hh = document.getElementsByTagName("html")[0];
	}
	tt.style.height = (hh.clientHeight + 1) + "px";
	//lth.style.height = (hh.clientHeight - 65) + "px";
	
	window.onresize = resizetable;

	window.focus();
}

//////////////////////////////////////////////////////////

function KcmsDoPostBack() 
{
	var theForm = document.getElementById('aspnetForm');
	if(!theForm)
	{
		theForm = document.getElementById('form1');
	}
	if(theForm)
	{
		theForm.submit();
	}
}
///////////////////////////////////////////////////////////
//global
function KcmsGoToLoc(loc)
{
	window.location = loc;
}


function KcmsFindParent(node, obj)
{
	var parent = obj.parentNode;
	while(parent.nodeName != node)
	{
		parent = parent.parentNode;
	}
	return parent;
}

function KcmsFindChild(node, obj)
{
	var parent = obj.parentNode;
	while(parent.nodeName != node)
	{
		parent = parent.parentNode;
	}
	return parent;
}

function KcmsGetEventSrcObj(ev)
{
	var srcobj;
	if(ev.srcElement)
	{
		srcobj = ev.srcElement;
	}
	else if(ev.target)
	{
		srcobj = ev.target;
	}
	return srcobj;
}

function KcmsOver(ev) //event
{
	var srcobj = KcmsGetEventSrcObj(ev);
	var trobj = KcmsFindParent("TR", srcobj);
	if(trobj)
	{
		trobj.className = "rowover";
	}
}

function KcmsOut(ev) //event
{
	var srcobj = KcmsGetEventSrcObj(ev);
	var trobj = KcmsFindParent("TR", srcobj);
	if(trobj)
	{
		trobj.className = "rowout";
	}
}

function KcmsRowclick(ev)
{
	var srcobj = KcmsGetEventSrcObj(ev);
	if(srcobj.nodeName == "INPUT")
	{
		return;
	}
	var trobj = KcmsFindParent("TR", srcobj);
	if(trobj != null)
	{
		var loc = document.getElementById("ctl00_ContentPlaceHolder1_hdnTargetPage").value;
		var pageid = trobj.id.split("-")[1];
		//var category = trobj.id.split("-")[2];
		window.location = loc + "?p=" + pageid + "&ret=" + window.location ;
	}
}
	
function KcmsSetSelRowEvents(row)
{
	for(e in row)
	{
		y = row[e];
		if((y.className != null) && (y.className == "selrow"))
		{
			if(y.addEventListener)
			{
				y.addEventListener('mouseover', KcmsOver, false);
				y.addEventListener('mouseout', KcmsOut, false);
				y.addEventListener('click', KcmsRowclick, false);
			}
			else
			{
				y.attachEvent("onmouseover", KcmsOver);
				y.attachEvent("onmouseout", KcmsOut);
				y.attachEvent("onclick", KcmsRowclick);
			}
		}
	}

}

function KcmsSetToolbarEvents(tds)
{
	for(e in tds)
	{
		y = tds[e];
		if((y.className != null) && (y.className == "toolbarbtn"))
		{
			if(y.addEventListener)
			{
				y.addEventListener('mouseover', KcmsToolbarover, false);
				y.addEventListener('mouseout', KcmsToolbarout, false);
			}
			else
			{
				y.attachEvent("onmouseover", KcmsToolbarover);
				y.attachEvent("onmouseout", KcmsToolbarout);
			}
		}
	}
}

function KcmsToolbarover(ev)
{
	var srcobj = KcmsGetEventSrcObj(ev);
	if(srcobj.nodeName != "TD")
	{
		var srcobj = KcmsFindParent("TD", srcobj);
	}
	srcobj.className = "toolbarbtnover";
}

function KcmsToolbarout(ev)
{
	var srcobj = KcmsGetEventSrcObj(ev);
	if(srcobj.nodeName != "TD")
	{
		var srcobj = KcmsFindParent("TD", srcobj);
	}
	srcobj.className = "toolbarbtn";
}

function KcmsChkSelectChanged(chkobject, itemid) 
{
	if(!this.KcmsChkSelected)
	{
		this.KcmsChkSelected = new Array();
	}
	if(chkobject.checked)
	{
		this.KcmsChkSelected[itemid] = "true";
	}
	else
	{
		delete this.KcmsChkSelected[itemid];
	}	
}

function KcmsChkActiveChanged(chkobject, itemid, original)
{
	if(!this.KcmsChkActive)
	{
		this.KcmsChkActive = new Array();
	}
	
	if(chkobject.checked == original)
	{
		delete this.KcmsChkActive[itemid];
	}
	else
	{
		this.KcmsChkActive[itemid] = chkobject.checked;
		
	}
}


function KcmsToolbarClick(cmd, question)
{
	if((question) && (question == "ask"))
	{
		if( ! confirm("Are you sure?"))
		{
			return;
		}
	}
	if(cmd == "logout")
	{
		window.location = "default.aspx?cmd=logout";
		return;
	}
	//fill command type
	var command = document.getElementById("ctl00_ContentPlaceHolder1_hdnCommand");
	if(!command)
	{
		command = document.getElementById("hdnCommand");
	}
	command.value = cmd;
	// fill params
	var paramholder = document.getElementById("ctl00_ContentPlaceHolder1_hdnParams");
	if(!paramholder)
	{
		paramholder = document.getElementById("hdnParams");
	}
	if(paramholder)
	{
		paramholder.value = "";
	}
	if(cmd == "save")
	{
		for(var itemid in this.KcmsChkActive)
		{
			paramholder.value += itemid + "-" + this.KcmsChkActive[itemid] + ";";	
		}
	}
	else if((cmd == "copy") || (cmd == "delete"))
	{
		for(var itemid in this.KcmsChkSelected)
		{
			paramholder.value += itemid + ";";
		}
	}
	//else if(cmd == "savedoc) do nothing, only postback
	KcmsDoPostBack();
}

function KcmsReload()
{
	window.location = window.location;
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////  IMAGE GALLERY ///////////////////////////////////////////////


var KcmsSelectedImage = null;
var KcmsSelectedFile = "";
function KcmsImageClick(obj, filename)
{
	if(KcmsSelectedImage)
	{
		KcmsSelectedImage.className = "imageholder";
	}
	KcmsSelectedFile = filename;
	KcmsSelectedImage = obj;
	obj.className = "imageholdersel";
}

function KcmsToolbarCmd(cmd, question)
{
	if((question) && (question == "ask"))
	{
		if(! confirm("Are you sure?"))
		{
			return;
		}
	}
	document.getElementById("ctl00_ContentPlaceHolder1_hdnSelectedFile").value = KcmsSelectedFile;
	document.getElementById("ctl00_ContentPlaceHolder1_hdnCommand").value = cmd;
	KcmsDoPostBack();
}



////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

/////